#!/bin/bash
e="\x1b[";c=$e"00m";y=$e"93;01m";cy=$e"96;01m";bb=$e"96;44;01m";r=$e"1;91m";g=$e"92;01m";m=$e"95;01m";
##########################################################
#                                                        #
#           Carliv Image Kitchen for Android             #
#       boot & recovery images (c)-2021 carliv.eu        #
#    including support for MTK powered phones images     #
#                                                        #
##########################################################
abort() { cd "$PWD"; echo " "; echo -e "$cy >>$c$r Exit script$c\n"; }
###########################################################
bin="$PWD/.bin";
chmod -R 755 "$bin"/*;
cd "$PWD";
unpackbootimg="$bin"/unpackbootimg;
###########################################################
clear;
echo -e "$bb
*****************************************************
*                                                   *
*        Carliv Image Kitchen for Android v2.3      *
*      boot & recovery images (c)2021 carliv.eu     *
*  including support for MTK powered phones images  *
*                 LINUX x86 version                 *
*                                                   *
*****************************************************$c
*             The unpacking images script           *
*****************************************************
";
###########################################################
if [ ! "$1" ]; 
	then
	echo -e "$r No image file selected. ERROR!$c";
	abort;
	exit 1;
fi;
if [ ! "$2" ]; 
	then
	echo -e "$r No folder specified for unpack. ERROR!$c";
	abort;
	exit 1;
fi;
###########################################################
file=$(basename "$1");
origfile=$(basename "$2");
filename="${origfile%.*}";
echo -e "Your image is$y $origfile$c";
###########################################################
echo " ";
wimage="$file";
###########################################################
echo " ";
echo -e "Creating the$y $filename$c folder....";
echo " ";
if [ -d "$filename" ];
	then
	rm -rf "$filename";
fi;
mkdir -p "$filename";
chmod 777 "$filename";
###########################################################
if [ ! -e "$wimage" ];
	then
	echo -e "$r No image to process. ERROR!$c";
	abort;
	exit 1;
fi;
echo -e "Unpacking the$y $file$c to$y $filename$c folder....";
echo " ";
$unpackbootimg -i "$wimage" -o "$filename";
cd "$filename";
comprfile=$(find . -name "ramdisk.*");
compress="${comprfile##*.}";
echo -e "Compression used:$y $compress ";
filecomp=ramdisk_compress;
if [ ! -e "$filecomp" ]
	then 
	echo "$compress" > "$filecomp" ;
fi;
mkdir -p ramdisk;
###########################################################
echo " ";
echo -e "Unpacking the$y ramdisk$c....";
cd ramdisk;
###########################################################
if [ "$compress" == "gz" ];
	then
	gzip -dcv "../ramdisk.gz" | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.gz";
	cd ../ ;
fi;
if [ "$compress" == "lzma" ];
	then
	xz -dcv "../ramdisk.lzma" | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.lzma";
	cd ../ ;
fi;
if [ "$compress" == "xz" ];
	then
	xz -dcv "../ramdisk.xz" | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.xz";
	cd ../ ;
fi;
if [ "$compress" == "bz2" ];
	then
	bzip2 -dcv "../ramdisk.bz2" | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.bz2";
	cd ../ ;
fi;
if [ "$compress" == "lzo" ];
	then
	lzop -dcv "../ramdisk.lzo" | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.lzo";
	cd ../ ;
fi;
if [ "$compress" == "lz4" ];
	then
	lz4 -dv "../ramdisk.lz4" stdout | cpio -i ;
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
	fi;
	cd ../ ;
	rm -f "ramdisk.lz4";
	cd ../ ;
fi;
if [ "$compress" == "bin" ];
	then
	  echo -e "$r Your ramdisk archive is corrupt or is packed with an unsupported archive format. ERROR!$c";
	  abort;
	  exit 1;
fi;
###########################################################
echo " ";
if [ ! -e "$filename"/ramdisk/sbin/recovery ];
	then
	if [ -e "$filename"/mtk ]; then
		echo "1" > "$filename"/mtk;
	fi;
fi;
if [ -e "$filename"/ramdisk/sbin/recovery ];
	then
	if [ -e "$filename"/mtk ]; then
		echo "2" > "$filename"/mtk;
	fi;
fi;
###########################################################
echo -e "Done! Your image is unpacked in$y $filename$c folder.";
echo " ";
